package Testing.Server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import Testing.Client.Adder;

public class RMI_Server extends UnicastRemoteObject implements Adder
{

	private static final long serialVersionUID = -4617066724092138267L;

	protected RMI_Server() throws RemoteException
	{
		super();
	}

	public static void main(String[] args)
	{
		// System.setSecurityManager(new RMISecurityManager());

		/*
		 * In Eclipse 1. Start RMIRegistry with a specified unique port number
		 * 2. Create a new project and import four required files 
		 * 3. Put the following line in "VM arguments" of your "AdderImpl" running
		 * configuratoin (Run-Run...). Replace ### with your Eclipse Project
		 * Name -Djava.security.policy="file:/${workspace_loc}/###/policy"
		 * -Djava.rmi.server.codebase="file:/${workspace_loc}/###/bin/" Remeber
		 * to set the main class as "AdderImpl". 4. Click the "Run" button. You
		 * shoud see : Created and registered AdderImpl object 5. Open the
		 * AdderClient file, right click, Run as -> Java Applicatoin
		 * 
		 * Alternately add the following line in your RMIserver:
		 * java.rmi.registry.LocateRegistry.createRegistry(port);
		 */
		try
		{
			java.rmi.registry.LocateRegistry.createRegistry(10001);
			RMI_Server server = new RMI_Server();
			Naming.rebind("rmi://localhost:10001/AdderImpl", server);
			System.out.println("Created and registered AdderImpl object");
		}
		catch (RemoteException e)
		{
			e.printStackTrace();
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}

	}

	@Override
	public int add(int x, int y) throws RemoteException
	{
		return x + y;
	}

}
